package com.codebo.yunpicturebackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codebo.yunpicturebackend.annotation.AuthCheck;
import com.codebo.yunpicturebackend.common.BaseResponse;
import com.codebo.yunpicturebackend.common.DeleteRequest;
import com.codebo.yunpicturebackend.common.ErrorCode;
import com.codebo.yunpicturebackend.common.ResultUtils;
import com.codebo.yunpicturebackend.constant.UserConstant;
import com.codebo.yunpicturebackend.exception.ThrowUtils;
import com.codebo.yunpicturebackend.model.dto.user.*;
import com.codebo.yunpicturebackend.model.entity.User;
import com.codebo.yunpicturebackend.model.vo.LoginUserVO;
import com.codebo.yunpicturebackend.model.vo.UserVO;
import com.codebo.yunpicturebackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController
{
    @Resource
    private UserService userService;

    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterDTO userRegisterDTO)
    {
        ThrowUtils.throwIf(userRegisterDTO == null, ErrorCode.PARAMS_ERROR, "注册参数为空");
        long userId = userService.userRegister(userRegisterDTO);
        return ResultUtils.success(userId);
    }

    @PostMapping("/login")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest httpServletRequest)
    {
        ThrowUtils.throwIf(userLoginDTO == null, ErrorCode.PARAMS_ERROR, "登录参数为空");
        LoginUserVO loginUserVO = userService.userLogin(userLoginDTO, httpServletRequest);
        return ResultUtils.success(loginUserVO);
    }

    @GetMapping("/get/login")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest httpServletRequest)
    {
        User user = userService.getcurrentUser(httpServletRequest);
        return ResultUtils.success(userService.getLoginUserVO(user));
    }

    @GetMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest httpServletRequest)
    {
        if (!userService.userLogout(httpServletRequest))
        {
            return ResultUtils.error(ErrorCode.OPERATION_ERROR, "登出失败");
        }
        return ResultUtils.success(true);
    }

    @GetMapping("/get/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<UserVO> getUserVOById(@RequestParam Long id)
    {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR, "id不合法");
        User user = userService.getUserById(id);
        UserVO userVO = userService.getUserVO(user);
        return ResultUtils.success(userVO);
    }

    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addUser(@RequestBody UserAddDTO userAddDTO)
    {
        ThrowUtils.throwIf(userAddDTO == null, ErrorCode.PARAMS_ERROR, "添加用户参数为空");
        long userId = userService.addUser(userAddDTO);
        return ResultUtils.success(userId);
    }

    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<User> getUserById(@RequestParam Long id)
    {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR, "id不合法");
        User user = userService.getUserById(id);
        return ResultUtils.success(user);
    }

    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest)
    {
        ThrowUtils.throwIf(deleteRequest == null, ErrorCode.PARAMS_ERROR, "删除用户参数为空");
        ThrowUtils.throwIf(deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR, "id不合法");
        boolean result = userService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }

    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateDTO userUpdateDTO)
    {
        ThrowUtils.throwIf(userUpdateDTO == null, ErrorCode.PARAMS_ERROR, "更新用户为空");
        boolean result = userService.updateUser(userUpdateDTO);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新失败");
        return ResultUtils.success(true);
    }

    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVO>> listUserVOByPage(@RequestBody UserQueryDTO userQueryDTO)
    {
        ThrowUtils.throwIf(userQueryDTO == null, ErrorCode.PARAMS_ERROR, "查询用户参数为空");
        int current = userQueryDTO.getCurrent();
        int pageSize = userQueryDTO.getPageSize();
        Page<User> page = userService.page(new Page<>(current, pageSize), userService.getQueryWrapper(userQueryDTO));
        List<UserVO> userVOList = userService.getUserVOList(page.getRecords());
        Page<UserVO> userVOPage = new Page<>(current, pageSize, page.getTotal());
        userVOPage.setRecords(userVOList);
        return ResultUtils.success(userVOPage);


    }


}
